package hn.cch.version.lts11.http;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodySubscriber;
import java.net.http.HttpResponse.BodySubscribers;
import java.net.http.HttpResponse.ResponseInfo;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;


public class SendAsyncTest {

    public static void main(String[] args) {
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest httpRequest = HttpRequest.newBuilder(URI.create("http://localhost:8080/text11")).build();


        BodyHandler<String> bodyHandler = responseInfo -> {
            if (responseInfo.statusCode() == 200) {
                System.out.println("OK");
                return BodySubscribers.ofString(Charset.defaultCharset());
            } else {
                System.out.println("NO");
                return BodySubscribers.replacing("text");
            }
        };
        httpClient.sendAsync(httpRequest, bodyHandler)
            .thenApply(HttpResponse::body).thenAccept(System.out::println).join();
    }

}